package org.linlinjava.litemall.wx.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.linlinjava.litemall.core.config.AlipayProperties;
import org.linlinjava.litemall.wx.service.AlipayService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**阿里支付  测试时候  沙箱环境 通过..
 * @author Suoron
 */

@RestController
@RequestMapping("wx/alipay")
public class AlipayController {

    @Resource
    AlipayService alipayService;
    @Resource
    AlipayProperties alipayProperties;


    @GetMapping("pay")
    public void testPay(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html; charset=utf-8");
            response.setContentType("text/html; charset=utf-8");
            response.getWriter().write(alipayService.getwayPay());
            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
            //return ResponseUtil.fail();
        }
    }

    /**
     *  支付
     * @param body  orderId 订单ID
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("nativePay")
    public void nativePay(@RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws IOException {
        XxlSsoUser currentUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userId = currentUser.getUserid();
        response.setContentType("text/html; charset=utf-8");
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write(alipayService.nativePay(Integer.parseInt(userId), body, request));
        response.getWriter().close();

    }

    /**
     * 支付保证金
     * @param body  disputeId 争议单ID
     * @param request
     * @param response
     * @return
     */
    @PostMapping("nativePayMargin")
    public Object nativePayMargin(@RequestBody String body, HttpServletRequest request, HttpServletResponse response) {
        return alipayService.nativePayMargin(body, request);

    }

    /**
     *  通知霜林
     * @param request  "sign_type"  "trade_no"    "out_trade_no"
     * @param response
     * @return
     */
    @PostMapping("shuanglin-notify")
    public String callback(HttpServletRequest request, HttpServletResponse response) {
        return alipayService.payNotify(request, response);
    }

}